package com.rcloud.server.sealtalk.controller;

import com.github.binarywang.java.emoji.EmojiConverter;
import com.rcloud.server.sealtalk.controller.param.CollectCommonWordsParam;
import com.rcloud.server.sealtalk.controller.param.CollectDelParam;
import com.rcloud.server.sealtalk.controller.param.UserCollectParam;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.manager.CollectManager;
import com.rcloud.server.sealtalk.model.dto.UserCollectDTO;
import com.rcloud.server.sealtalk.model.response.APIResult;
import com.rcloud.server.sealtalk.model.response.APIResultWrap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Jianlu.Yu
 * @Date: 2020/8/20
 * @Description:
 * @Copyright (c) 2020, rongcloud.cn All Rights Reserved
 */
@Slf4j
@Api(tags = "用户收藏相关")
@RestController
@RequestMapping("/user/collect")
public class UserCollectController extends BaseController {

    @Autowired
    private CollectManager collectManager;


    /**
     * 获取用户的收藏列表
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取用户的收藏列表")
    @ApiImplicitParam(name = "type", value = "分类条件 1.文本, 2图片, 3视频, 4定位，5文件 6语音 7自定义", defaultValue = "0", dataType = "Integer")
    public APIResult<Object> list(@RequestParam Integer type) {
        Integer currentUserId = getCurrentUserId();
        EmojiConverter emojiConverter = EmojiConverter.getInstance();
        List<UserCollectDTO> userCollectDTOS = collectManager.queryCollectList(type, currentUserId);
        for (UserCollectDTO dto :userCollectDTOS) {
            dto.setContent(emojiConverter.toUnicode(dto.getContent()));
        }
        return APIResultWrap.ok(userCollectDTOS);
    }


    /**
     * 添加用户收藏
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加用户收藏")
    public APIResult<Object> add(@RequestBody UserCollectParam param) throws ServiceException {
        Integer currentUserId = getCurrentUserId();
        collectManager.saveCollect(param, currentUserId);
        return APIResultWrap.ok("", "添加成功");
    }

    /**
     * 编辑用户收藏
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "编辑用户收藏")
    public APIResult<Object> edit(@RequestBody UserCollectParam param) throws ServiceException {
        collectManager.editCollect(param);
        return APIResultWrap.ok("", "修改成功");
    }

    /**
     * 删除用户收藏
     */
    @PostMapping(value = "/removeCollect")
    @ApiOperation(value = "删除用户收藏")
    public APIResult<Object> removeCollect(@RequestBody CollectDelParam param) throws ServiceException {
        collectManager.removeCollect(param.getIds());
        return APIResultWrap.ok("", "删除成功");
    }


    /**
     * 批量设置收藏信息为常用语
     */
    @PostMapping(value = "/setCollectCommonWords")
    @ApiOperation(value = "批量设置收藏信息为常用语")
    public APIResult<Object> setCollectCommonWords(@RequestBody CollectCommonWordsParam param) throws ServiceException {
        collectManager.setCollectCommonWords(param.getIds(), param.getSetWordsType());
        return APIResultWrap.ok("", "设置成功");
    }
    /**
     * 获取用户的收藏列表
     */
    @GetMapping(value = "/queryCommonWordsList")
    @ApiOperation(value = "获取用户的常用语列表信息")
    public APIResult<Object> queryCommonWordsList() {
        EmojiConverter emojiConverter = EmojiConverter.getInstance();
        Integer currentUserId = getCurrentUserId();
        List<UserCollectDTO> userCollectDTOS = collectManager.queryCommonWordsList(currentUserId);
        for (UserCollectDTO dto :userCollectDTOS) {
            dto.setContent(emojiConverter.toUnicode(dto.getContent()));
        }
        return APIResultWrap.ok(userCollectDTOS);
    }


}
